package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysOrganization;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.SysOrganizationMapper;
import com.hzhc.service.SysOrganizationService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.TreeVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 公司组织机构 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-04-07
 */
@Service
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements SysOrganizationService {
    @Override
    public List<SysOrganization> getSysOrganizationJson(SysOrganization organization) {
        if (StringUtils.isBlank(organization.getName())) {
            return list(Wrappers.<SysOrganization>lambdaQuery()
                    .eq(SysOrganization::getStatus, 1)
                    .eq(organization.getPid() != null, SysOrganization::getPid, organization.getPid()));
        } else {
            return list(Wrappers.<SysOrganization>lambdaQuery()
                    .eq(SysOrganization::getStatus, 1)
                    .like(SysOrganization::getName, organization.getName())
                    .eq(organization.getPid() != null, SysOrganization::getPid, organization.getPid()));
        }
    }

    @Override
    public List<TreeVo> getOrganizationTree(Long pid) {
        List<TreeVo> retList = new ArrayList<TreeVo>();
        List<SysOrganization> list = list();
        Map<Long, List<SysOrganization>> organizationMap = list.stream().collect(Collectors.groupingBy(SysOrganization::getPid));
        // 是否需要展示最高节点
        List<SysOrganization> glist = organizationMap.get(pid);
        if (glist != null) {
            getOrganizationTreeList(retList, organizationMap, glist);
        }
        return retList;
    }

    @Override
    public List<SysOrganization> getOrganizationListAll(SysOrganization vo) {
        return baseMapper.selectSysOrganizationAll(vo);
    }

    @Override
    public ResultUtil<T> saveOrganization(SysOrganization vo) {
        ResultUtil<T> r = checkOrg(vo);
        if (!ResultUtilCodeEnum.SUCCESS.getCode().equals(r.getCode())) {
            return r;
        }
        vo.setCreateTime(LocalDateTime.now());
        boolean b = save(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateOrganization(SysOrganization vo) {
        ResultUtil<T> r = checkOrg(vo);
        if (!ResultUtilCodeEnum.SUCCESS.getCode().equals(r.getCode())) {
            return r;
        }
        boolean b = updateById(vo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteOrg(SysOrganization vo) {
        boolean b = remove(Wrappers.<SysOrganization>lambdaUpdate()
                .eq(SysOrganization::getId, vo.getId()));
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> checkOrg(SysOrganization vo) {
        if (StrUtil.isBlank(vo.getCode())) {
            return ResultUtil.ok();
        }
        int i = count(Wrappers.<SysOrganization>lambdaQuery()
                .eq(SysOrganization::getCode, vo.getCode())
                .ne(vo.getId() != null, SysOrganization::getId, vo.getId()));
        return i > 0 ? ResultUtil.build(ResultUtilCodeEnum.ORG_CODE_EXIST) : ResultUtil.ok();
    }

    private void getOrganizationTreeList(List<TreeVo> retList, Map<Long, List<SysOrganization>> organizationMap, List<SysOrganization> glist) {
        for (SysOrganization organization : glist) {
            TreeVo treeVo = new TreeVo();
            treeVo.setId(organization.getId().intValue());
            treeVo.setLabel(organization.getName());
            treeVo.setDisabled(false);
            treeVo.setChildren(getTreeChildren(organizationMap, organization.getId()));
            retList.add(treeVo);
        }
    }

    private List<TreeVo> getTreeChildren(Map<Long, List<SysOrganization>> organizationMap, Long pid) {
        List<TreeVo> retList = new ArrayList<TreeVo>();
        List<SysOrganization> childlist = organizationMap.get(pid);
        if (childlist != null && !childlist.isEmpty()) {
            getOrganizationTreeList(retList, organizationMap, childlist);
            return retList;
        }
        return null;
    }
}
